import Store2 from 'store2'
import Config from '@/utils/config.js'

const User = {
  state: {
    token: Store2(Config.TOKEN_STORAGE_KEY), // 登陆之后保存token
    userinfo: {},
    mallinfo: {},
    userrole: []
  },
  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_USERINFO: (state, info) => {
      Object.assign(state.userinfo, info)
    },
    SET_MALLINFO: (state, info) => {
      Object.assign(state.mallinfo, info)
    },
    CLEAR_USERINFO: (state) => {
      state.userinfo = {}
    },
    CLEAR_MALLINFO: (state) => {
      state.mallinfo = {}
    },
    SET_ROLES: (state, roles) => {
      state.userrole = roles
    },
    CLEAR_ROLES: (state) => {
      state.userrole = []
    }
  },

  actions: {
    commitRole ({ commit }, roles) {
      commit('SET_ROLES', roles)
    },
    removeRole ({ commit }) {
      commit('CLEAR_ROLES')
    },
    saveToken ({ commit }, token) {
      commit('SET_TOKEN', token)
    },
    removeToken ({ commit }) {
      commit('SET_TOKEN', null)
    },
    commitUserInfo ({ commit }, info) {
      commit('SET_USERINFO', info)
    },
    commitMallInfo ({ commit }, info) {
      commit('SET_MALLINFO', info)
    },
    clearUserInfo ({ commit }, info) {
      commit('CLEAR_USERINFO', info)
    },
    clearMallInfo ({ commit }, info) {
      commit('CLEAR_MALLINFO', info)
    }
  }
}

export default User
